package com.prs.crm.action.oa.schedule;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.schedule.Diary;
import com.prs.crm.domain.oa.schedule.DiaryReply;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.oa.schedule.DiaryReplyService;
import com.prs.crm.service.oa.schedule.DiaryService;
/**
 * @author YangHao
 * @E-Mail:450098606@qq.com
 * @qq:450098606
 * @version 创建时间：2012-4-16 15:38:48 日记
 */
public class DiaryAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private Diary diary;
	private DiaryService diaryService;
	
	private PaginationSupport pagination;
	private Integer[] ids;
	
	private DiaryReply diaryReply;
	private DiaryReplyService diaryReplyService;
	
	private File upload;
	private String uploadFileName;
	private String title;
	private List<DiaryReply> reply;
	private String titles;
	private String date;
	private PersonService personService;
	public PersonService getPersonService() {
		return personService;
	}
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	public String getTitles() {
		return titles;
	}
	public void setTitles(String titles) {
		this.titles = titles;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public List<DiaryReply> getReply() {
		return reply;
	}
	public void setReply(List<DiaryReply> reply) {
		this.reply = reply;
	}
	public DiaryReply getDiaryReply() {
		return diaryReply;
	}
	public void setDiaryReply(DiaryReply diaryReply) {
		this.diaryReply = diaryReply;
	}
	public DiaryReplyService getDiaryReplyService() {
		return diaryReplyService;
	}
	public void setDiaryReplyService(DiaryReplyService diaryReplyService) {
		this.diaryReplyService = diaryReplyService;
	}
	public Diary getDiary() {
		return diary;
	}
	public void setDiary(Diary diary) {
		this.diary = diary;
	}
	public DiaryService getDiaryService() {
		return diaryService;
	}
	public void setDiaryService(DiaryService diaryService) {
		this.diaryService = diaryService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	@Action("listDiary")
	public String listDiary(){
		Person curPerson=AppContext.getAccountContext().currentUser();
		List<Diary> di=this.getDiaryService().getAllDiary();
		for (Diary ob : di) {
			Person per=this.getPersonService().getPersonById(curPerson.getId());
			if (per!=null) {
				if (per.getSubordinateId()==null ||"".equals(per.getSubordinateId().trim())) {
					if (ob.getRecorder().equals(curPerson)) {
						ob.setShowState(curPerson.getId()+":1");
						this.getDiaryService().submit(ob);
					}
				}else{
					String[] pers=per.getSubordinateId().split(",");
					for (String p : pers) {
						if (ob.getRecorder().getId().equals(Integer.parseInt(p))) {
							ob.setShowState(curPerson.getId()+":1");
							this.getDiaryService().submit(ob);
						}
					}
				}
			}
		}
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(Diary.class);
		criteria.add(Restrictions.eq("showState", curPerson.getId()+":1"));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDiaryService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("viewDiaryReply")
	public String viewDiaryReply(){
		this.reply=this.diary.getDiaryReply();
		this.title=this.diary.getTitle();
		this.diary.setReadNum(diary.getReadNum()+1);
		this.diaryService.submit(diary);
		return SUCCESS;
	}
	
	@Action("editDiary")
	public String edit(){
		Date dt=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		String dd=sf.format(dt);
		titles=AppContext.getAccountContext().currentUser().getResume().getName()+"("+dd+")";
		date=dd;
		return SUCCESS;
	}
	@Action("editDiaryReply")
	public String editDiaryReply(){
		this.title=this.diary.getTitle();
		return SUCCESS;
	}
	@Action(value = "saveDiary", results = {
			@Result(name = "success", type = "redirect", location = "listDiary"),
			@Result(name = "error", type = "chain", location = "editDiary") })
	public String saveDiary(){
		if (upload!=null) {
			this.diary.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		
		Person person=new Person();
		person=AppContext.getAccountContext().currentUser();
		diary.setLastName(person.getResume().getName());
		diary.setLastTime(new Date());
		if (diary.getId()!=null) {
			Person per=new Person();
			per=AppContext.getAccountContext().currentUser();
			Date date=new Date();
			SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
			diary.setType(1);
			diary.setUpPerosn(per.getResume().getName());
			diary.setUpTime(sf.format(date));
			this.getDiaryService().submit(diary);
		}
		else {
			diary.setSpkNum(0);
			diary.setReadNum(0);
			diary.setType(0);
			this.getDiaryService().save(diary);
			this.setPrompt("添加成功！");
		}
		
		return SUCCESS;
	}
	@Action(value = "saveDiaryReply", results = {
			@Result(name = "success", type = "redirect", location = "viewDiaryReply?diary=${diary.getId()}"),
			@Result(name = "error", type = "chain", location = "editDiaryReply?diary=${diary.getId()}") })
	public String saveDiaryReply(){
		if (upload!=null) {
			this.diaryReply.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		diary.setSpkNum(diary.getSpkNum()+1);
		Person person=AppContext.getAccountContext().currentUser();
		diary.setLastName(person.getResume().getName());
		diary.setLastTime(new Date());
		this.getDiaryService().submit(diary);
		diaryReply.setDiary(diary);
		this.diaryReply.setTitle(title);
		this.diaryReplyService.save(diaryReply);
		return SUCCESS;
	}
	
	@Action(value = "removeDiary", results = {
			@Result(name = "success", type = "redirect", location = "listDiary"),
			@Result(name = "error", type = "chain", location = "listDiary") })
	public String removeDiary(){
		if (diaryReply!=null) {
			this.getDiaryReplyService().remove(diaryReply);
		}
		this.getDiaryService().remove(diary);
		return SUCCESS;
	}
	@Action(value = "removeDiaryReply", results = {
			@Result(name = "success", type = "redirect", location = "listDiary"),
			@Result(name = "error", type = "chain", location = "listDiary") })
	public String removeDiaryReply(){
		this.getDiaryReplyService().remove(diaryReply);
		diaryReply.setDiary(diaryReply.getDiary());
		diaryReply.getDiary().setSpkNum(diaryReply.getDiary().getSpkNum()-1);
		this.getDiaryService().submit(diaryReply.getDiary());
		return SUCCESS;
	}
}
